# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import codecs
import json

class DdbookPipeline:
    fp = None

    # 重写父类的方法：该方法只会在开始爬虫的时候被调用一次
    def open_spider(self, spider):
        print("###########################################")
        print("开始爬虫。。。")
        self.fp = codecs.open("C:\\Users\\DELL\\Desktop\\ddbook.csv", 'w', encoding='utf8')
        # self.fp.write('[')

    def process_item(self, item, spider):
        json_str = json.dumps(dict(item), ensure_ascii=False) + ",\n"
        self.fp.write(json_str)
        return item

    def close_spider(self, spider):
        print("###########################################")
        print("爬虫结束")

        # self.fp.write(']')
        self.fp.close()

